package com.aide.simplification.global.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.ecs.html.Input;
import org.apache.ecs.html.Script;

public class SuggestTag extends TagSupport {
	private static final long serialVersionUID = 1933404498058164295L;
	private String name = "";
	private String id = "";
	private String style = "";
	private String nameVal = "";
	private String idVal = "";
	private int maxsize = 10;
	private String function = "";
	
	public int doEndTag() throws JspException {
		//ServletRequest request = pageContext.getRequest();
		//ServletResponse response = pageContext.getResponse();
		JspWriter out = pageContext.getOut();
		//ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext());
		
		Script script = new Script();
		script.setType("text/javascript");
		
		StringBuffer scripttext= new StringBuffer();
		scripttext.append("var $j;if(!$j){$j = jQuery.noConflict()};");
		scripttext.append(" var param = {\n");
		scripttext.append("inputname:\""+name+"\",\n");
		scripttext.append("selfname:\""+name+"obj\",\n");
		if(id.equals("")){
			id=name+"value";
		}
		scripttext.append("valuename:\""+id+"\",\n");
		scripttext.append("size:"+maxsize+",\n");
		scripttext.append("getDataFunc:\""+function+"\"\n");
		scripttext.append("};\n");
		scripttext.append("var "+name+"obj = new Suggest(param);\n");
		//scripttext.append("$(document).ready(function(){$('body').click(function(){"+name+"obj.hiddenSuggest();}); });");
		script.setTagText(scripttext.toString());
		Input inputname = new Input();
		if(!"".equals(style)){
			inputname.setStyle(style);
		}
		inputname.setType("text");
		inputname.setName(name);
		inputname.setID(name);
		inputname.setOnKeyUp(name+"obj.resetReading()");
		inputname.setValue(nameVal);
		inputname.addAttribute("autocomplete", "off");
		
		
		
		Input inputid = new Input();
		inputid.setType("hidden");
		inputid.setName(id);
		inputid.setID(id);
		inputid.setValue(idVal);
		
		try {
			out.println(script.toString());
			out.println(inputname.toString());
			out.println(inputid.toString());
		} catch (IOException e) {
		}
		return EVAL_PAGE;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getNameVal() {
		return nameVal;
	}

	public void setNameVal(String nameVal) {
		this.nameVal = nameVal;
	}

	public String getIdVal() {
		return idVal;
	}

	public void setIdVal(String idVal) {
		this.idVal = idVal;
	}

	public int getMaxsize() {
		return maxsize;
	}

	public void setMaxsize(int maxsize) {
		this.maxsize = maxsize;
	}

	public String getFunction() {
		return function;
	}

	public void setFunction(String function) {
		this.function = function;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	

	
}
